
with open('input.txt') as file:
    [start, buses] = file.read().splitlines()
    start = int(start)
    buses = [int(b) for b in buses.split(',') if b != 'x']

    min_arrival = float('inf')
    min_bus = None

    for b in buses:
        next_arrival = b - start % b
        if next_arrival < min_arrival:
            min_arrival, min_bus = next_arrival, b
    
    print(min_bus * min_arrival)